<template>
    <a-modal :keyboard="false" :maskClosable="false" title="关键词管理" :open="visible" :width="600" :confirmLoading="loading" centered @ok="handleSubmit" @cancel="handleCancel">
        <a-form layout="vertical" ref="formRef" :model="modelRef">
            <a-row :gutter="16" type="flex" justify="start">
                <a-col :xs="24" :sm="24" :md="24">
                    <a-form-item
                        label="关键词名称"
                        name="word"
                        :rules="[
                            {
                                required: true,
                                message: '请输入关键词名称',
                            },
                        ]"
                    >
                        <a-input v-model:value="modelRef.word" />
                    </a-form-item>
                </a-col>
                <a-col :xs="12" :sm="12" :md="12">
                    <a-form-item
                        label="关键词词性"
                        name="wordClassId"
                        :rules="[
                            {
                                required: true,
                                message: '请选择关键词词性',
                            },
                        ]"
                    >
                        <a-radio-group v-model:value="modelRef.wordClassId" :options="dictTypes"></a-radio-group>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-modal>
</template>

<script lang="ts" setup>
    import { reactive, ref, watchEffect, watch } from 'vue';
    import { message } from 'ant-design-vue';
    import { cloneDeep, isUndefined } from 'lodash-es';
    import { dictDataListByDictType } from '@/api/user/dict';
    import { createSensitiveWords, updateSensitiveWords } from '@/api/smss/keyword';

    const visible = ref<boolean>(false);

    const emit = defineEmits(['cancel', 'success']);

    const loading = ref(false);
    const formRef = ref<any>(null);
    const defaultForm = {};
    const modelRef = reactive<any>(cloneDeep(defaultForm));
    const dictTypes = ref<any[]>([]);
    const handleCancel = () => {
        formRef.value.resetFields();
        emit('cancel');
        visible.value = false;
        Object.keys(modelRef).forEach(key => {
            delete modelRef[key];
        });
        Object.assign(modelRef, cloneDeep(defaultForm));
    };

    const handleSubmit = async () => {
        formRef.value.validate().then(res => {
            loading.value = true;
            const { wordClassId, ...params } = modelRef;
            params.wordClassId = wordClassId;
            params.wordClass = dictTypes.value.find(item => item.value === params.wordClassId)?.label;

            (!isUndefined(params.id) ? updateSensitiveWords : createSensitiveWords)(params)
                .then(res => {
                    if (res.code == 200) {
                        message.success('操作成功');
                        emit('success');
                        handleCancel();
                    }
                })
                .finally(() => {
                    loading.value = false;
                });
        });
    };
    const init = async data => {
        console.log(data);
        Object.assign(modelRef, data);

        const responseData: any = await dictDataListByDictType({
            dictType: 'word_class',
        });
        dictTypes.value = responseData.data;

        visible.value = true;
    };

    defineExpose({
        init,
    });
</script>
